package com.xbongbong.parent.field.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.parent.field.FieldAnalyseService;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.model.DepartmentModel;
import com.xbongbong.saas.util.UserFieldUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/07 14:25
 */
@Service("deptFieldAnalyseServiceImpl")
public class DeptFieldAnalyseServiceImpl implements FieldAnalyseService {

    @Resource
    private DepartmentModel departmentModel;

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.DEPT.getType());
    }

    @Override
    public Object analysisData(Long formId, Integer businessType, Integer saasMark, PaasFormDataEntityExt paasFormDataEntityExt, JSONObject data, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long departmentId = data.getLong(fieldAttrEntity.getAttr());
        if (Objects.nonNull(departmentId)) {
            DepartmentEntity departmentEntity = departmentModel.getByKey(departmentId, paasFormDataEntityExt.getCorpid());
            if (Objects.nonNull(departmentEntity)) {
                return departmentEntity.getName();
            }
        }
        return null;
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
       return UserFieldUtil.getUserIdAndDeptIdList(filterCondition.getValue(), filterCondition.getSymbol());
    }
}
